package login.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import commons.pojo.Users;
import login.dao.LoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @Autowired
    private LoginMapper loginMapper;
    // 获取当前登录用户名
    @RequestMapping("/loginUser")
    public int getUsername(){
        // 1.获取会话对象
        SecurityContext context = SecurityContextHolder.getContext();
        // 2.获取认证对象
        Authentication authentication = context.getAuthentication();
        // 3.获取登录用户信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String username = userDetails.getUsername();
        QueryWrapper<Users> name = new QueryWrapper<Users>().eq("name", username);
        Users users = loginMapper.selectOne(name);


        return users.getId();
    }
}
